package com.atguigu.system.controller;

import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.vo.AssignRoleVo;
import com.atguigu.model.vo.SysRoleQueryVo;
import com.atguigu.system.execption.GuiguException;
import com.atguigu.system.service.SysRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {


    @Autowired
    SysRoleService sysRoleService;

    @ApiOperation("根据ID查询单条数据")
    @GetMapping("/getById/{id}")
    public Result<SysRole> getById(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        if(sysRole!=null){
            return Result.ok(sysRole);
        }else{
            return Result.fail();
        }
    }

    @ApiOperation("根据ID更新角色")
    @PutMapping("/updateById")
    public Result updateById(@RequestBody SysRole sysRole) {
        sysRole.setUpdateTime(null);
        sysRoleService.updateById(sysRole);
        return Result.ok();
    }

    @ApiOperation("根据ID删除单条数据")
    @DeleteMapping("/removeById/{id}")
    public Result removeById(@PathVariable Long id) {
        sysRoleService.removeById(id);
        return Result.ok();
    }

    @ApiOperation("根据多条ID删除多条数据")
    @DeleteMapping("/removeBatchById")
    public Result removeBatchById(@RequestBody List<Long> ids){
        sysRoleService.removeByIds(ids);
        return Result.ok();
    }

    @ApiOperation("保存角色")
    @PostMapping("/save")
    public Result save(@RequestBody SysRole sysRole) {
        sysRoleService.save(sysRole);
        return Result.ok();
    }


    @ApiOperation(value = "分页查询")
    @GetMapping("/{page}/{size}")
    public Result<IPage<SysRole>> findPage(HttpServletRequest request,
        @ApiParam(name = "page", value = "当前页", required = true) @PathVariable Long page,
        @ApiParam(name = "size", value = "每页条数", required = true) @PathVariable Long size,
        @ApiParam(name = "queryVo", value = "查询条件", required = false) SysRoleQueryVo queryVo) {

        String host = request.getHeader("Host");
        System.out.println("host = " + host);

        //1.接收请求参数
        IPage<SysRole> pageParam = new Page<>(page,size);

        //2.调用业务层
        // ①可以选择调用框架父接口提供的方法完成分页查询
        // ②也可以调用自定义分页查询方法
        IPage<SysRole> pageResult = sysRoleService.findPage(pageParam,queryVo);

        //3.返回结果
        return Result.ok(pageResult);
    }

    @ApiOperation(value = "获取所有角色")
    @GetMapping("/list")
    public Result<List<SysRole>> list() {
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }

    @ApiOperation("根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String,Object> map = sysRoleService.getRolesByUserId(userId);
        return Result.ok(map);
    }

    @ApiOperation("根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssignRoleVo assignRoleVo){
        sysRoleService.doAssign(assignRoleVo);
        return Result.ok();
    }
}
